import { ref } from 'vue'
import { adapter, request } from '@/request'
import { negativeWordsByGet } from '@/core/api/promotion'
import type { Pagination, RequestWrapper, WithTotalSize } from '@/core/interfaces/common'
import type { NegativeWord } from '@/core/interfaces/negative'
import type { MatchTypeEnum } from '@/core/enums/negative'
import type { PromotionDetail } from '@/services/promotion/getPromotionDetail'
import type { Creative } from '@/core/interfaces/promotion'
import type { ScopeTypeEnum } from '@/core/enums/negative'

interface Params extends Pagination {
  orderId: PromotionDetail['orderId']
  name: NegativeWord['name']
  orderWordId?: Creative['orderWordId']
  matchType?: MatchTypeEnum
  scopeType?: ScopeTypeEnum
}

export default function getNegativeWords() {
  const service = adapter<RequestWrapper<WithTotalSize<Record<'negativeWords', NegativeWord[]>>>>(request)
  const loading = ref(false)
  const response = ref<WithTotalSize<Record<'negativeWords', NegativeWord[]>>>({
    negativeWords: [],
    totalSize: 0
  })

  const fetch = (params: Params) => {
    const { url, method } = negativeWordsByGet()
    loading.value = true

    return service({
      config: {
        url,
        method,
        params
      },
      callback: (res) => {
        response.value = res.data.data
      },
      error: (err) => {
        console.log(err)
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [response, fetch, loading] as const
}
